package com.ncloud.resman.service.impl;

import com.ncloud.resman.entity.VirtualDisk;
import com.ncloud.resman.mapper.VirtualDiskMapper;
import com.ncloud.resman.service.SequenceService;
import com.ncloud.resman.service.VirtualDiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by shiqiang on 2017/8/28.
 */
@Service
public class VirtualDiskServiceImpl implements VirtualDiskService{
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private VirtualDiskMapper virtualDiskMapper;
    @Override
    public int insertVirtualDisk(VirtualDisk virtualDisk) {
        if(virtualDisk.getDiskId() == null){
            Long id = sequenceService.getSequence("disk_id_seq");
            virtualDisk.setDiskId(id);
        }
        if(virtualDisk.getCreateDate()==null){
            virtualDisk.setCreateDate(new Date());
        }
        return virtualDiskMapper.insertVirtualDisk(virtualDisk);
    }

    @Override
    public int updateVirtualDisk(VirtualDisk virtualDisk) {
        if(virtualDisk.getUpdateDate()==null){
            virtualDisk.setUpdateDate(new Date());
        }
        return virtualDiskMapper.updateVirtualDisk(virtualDisk);
    }

    @Override
    public VirtualDisk getVirtualDiskById(Long diskId) {
        return virtualDiskMapper.getVirtualDiskById(diskId);
    }

    @Override
    public List<VirtualDisk> getVirtualDiskByVirtualId(Long virtualId) {
        return virtualDiskMapper.getVirtualDiskByVirtualId(virtualId);
    }

    @Override
    public int deleteVirtualDisk(Long diskId) {
        return virtualDiskMapper.deleteVirtualDisk(diskId);
    }

    @Override
    public int updateVirtualDiskState(Long diskId, String state) {
        return virtualDiskMapper.updateVirtualDiskState(diskId,state);
    }

    @Override
    public List<VirtualDisk> getVirtualDiskByHostIp(String hostIp) {
        return virtualDiskMapper.getVirtualDiskByHostIp(hostIp);
    }

    @Override
    public int deleteVirtualDiskByVirtualId(Long virtualId) {
        return virtualDiskMapper.deleteVirtualDiskByVirtualId(virtualId);
    }
}
